﻿using System;
using CardLogistics.Model.Sys;
using CardLogistics.Web.Components;
using CardLogistics.DAL.Sys;

namespace CardLogistics.Web.MainPage.SystemMgr.SysUser
{
    public partial class UpPwd : BasePage
    {
        private static readonly CardLogistics.DAL.Sys.Sys_SysUserInfoData DAL = new DAL.Sys.Sys_SysUserInfoData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserName.Value=LoggedUser.USERNAME; //用户名
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_UpPwd_Click(object sender, EventArgs e)
        {
            try
            {
                string loginName = UserName.Value; //用户名
                string userPwd = UserPwd.Value; //原始密码
                string newUserPwd = NewUserPwd.Value; //新密码
                string newUserPwdOk = NewUserPwdOk.Value; //确认密码
                Sys_SysUserInfo user = new Sys_SysUserInfoData().GetModel(loginName, new DESC().Encrypto(userPwd));
                if (user != null)
                {
                    if (user.USERSTATUS == 0)//0正常
                    {
                         if (newUserPwdOk !=newUserPwd)
                        {
                             ShowMessageBox("确认密码与新密码不一致,请重新输入！");
                             NewUserPwd.Value = "";//清空新密码框
                             NewUserPwdOk.Value = "";//清空确认密码框
                             UserPwd.Focus();
                        }
                        else
                        { 
                            //修改用户密码
                            int result = DAL.UpdatePassword(loginName, new DESC().Encrypto(newUserPwdOk));
                            if (result>0)
                            {
                                 ShowMessageBox("用户密码修改成功！");
                                 ReturnListInfo();
                            }
                            else
                            {
                                ShowMessageBox("用户密码修改失败！");
                            }
                        }
                    }
                }
                else
                {
                    ShowMessageBox("原始密码输入错误,请重新输入！");
                    UserPwd.Value = "";//清空原始密码框
                    UserPwd.Focus();
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
    }
}